Ext.onReady(function(){
    Ext.QuickTips.init();
    Ext.BLANK_IMAGE_URL = 'ext3.2/resources/images/default/s.gif';
    var gridTitle = '电子书管理';
    var findall = "corpPublication_all.do";
    var getNewIdUrl = "corpPublication_newId.do";
    var addMsg = 'corpPublication_add.do';
    var deleteMsg = 'corpPublication_delete.do';
    var modifyMsg = 'corpPublication_modify.do';
    
    var renderTo = 'corppublication_grid';
    var seqValue = "corp_publication_seq"; // 序列号名称
    var identityName = "id"; // 主键名称
    var pageSize = 18;
    var gridHeight = 500;
    
    var sm = new Ext.grid.CheckboxSelectionModel();
    var cm = new Ext.grid.ColumnModel([sm, {
        header: "编号",
        width: 100,
        dataIndex: 'id'
    }, {
        header: "名称",
        width: 100,
        sortable: true,
        dataIndex: 'title'
    }, {
        header: "电子书类型",
        width: 100,
        dataIndex: 'typeId'
    }, {
        header: "企业ID",
        width: 100,
        dataIndex: 'corpId'
    }, {
        header: "书路径",
        width: 100,
        dataIndex: 'thumbPath'
    }, {
        header: "长度",
        width: 100,
        dataIndex: 'width'
    }, {
        header: "高度",
        width: 100,
        dataIndex: 'height'
    }, {
        header: "背景声音",
        width: 100,
        dataIndex: 'backgroundSound'
    }, {
        header: "是否重复",
        width: 100,
        dataIndex: 'backgroundRepeat'
    }, {
        header: "背景透明度",
        width: 100,
        dataIndex: 'backgroundAlpha'
    }, {
        header: "是否重复播放",
        width: 100,
        dataIndex: 'sounLoop'
    }, {
        header: "密码",
        width: 100,
        dataIndex: 'password'
    }, {
        header: "联系电话",
        width: 100,
        dataIndex: 'hits'
    }, {
        header: "评论次数",
        width: 100,
        dataIndex: 'comments'
    }, {
        header: "是否审核",
        width: 100,
        dataIndex: 'isAuditing'
    }, {
        header: "新增时间",
        width: 100,
        dataIndex: 'createDate'
    }, {
        header: "修改时间",
        width: 100,
        dataIndex: 'modDate'
    }, {
        header: "封底图",
        width: 100,
        dataIndex: 'endPath'
    }, {
        header: "背景图",
        width: 100,
        dataIndex: 'background'
    }, {
        header: "广告宽度",
        width: 100,
        dataIndex: 'adWidth'
    }, {
        header: "广告高度",
        width: 100,
        dataIndex: 'adHeight'
    }, {
        header: "总页数",
        width: 100,
        dataIndex: 'pagesCount'
    }, {
        header: "电子书大小",
        width: 100,
        dataIndex: 'pubmemSize'
    }, {
        header: "是否有效",
        width: 100,
        dataIndex: 'isValid'
    }, {
        header: "是否推荐",
        width: 100,
        dataIndex: 'isCommend'
    }, {
        header: "物理路径",
        width: 100,
        dataIndex: 'pubPath'
    }, {
        header: "出版类型",
        width: 100,
        dataIndex: 'pubType'
    }, {
        header: "杂志页数",
        width: 100,
        dataIndex: 'pubMagazineCount'
    }, {
        header: "杂志放大",
        width: 100,
        dataIndex: 'pubZoom'
    }, {
        header: "背景图大小",
        width: 100,
        dataIndex: 'pubBackgroudSize'
    }, {
        header: "广告大小",
        width: 100,
        dataIndex: 'pubadSize'
    }, {
        header: "密码有效期",
        width: 100,
        dataIndex: 'pwdLapseDate'
    }, {
        header: "密码起始页",
        width: 100,
        dataIndex: 'pwdStartPage'
    }, {
        header: "是否企业",
        width: 100,
        dataIndex: 'isCommpany'
    }]);
    
    var Struct = Ext.data.Record.create([{
        name: 'id',
        type: 'int'
    }, {
        name: 'title',
        type: 'string'
    }, {
        name: 'typeId',
        type: 'string'
    }, {
        name: 'corpId',
        type: 'string'
    }, {
        name: 'thumbPath',
        type: 'string'
    }, {
        name: 'width',
        type: 'int'
    }, {
        name: 'height',
        type: 'int'
    }, {
        name: 'backgroundSound',
        type: 'string'
    }, {
        name: 'backgroundRepeat',
        type: 'int'
    }, {
        name: 'backgroundAlpha',
        type: 'int'
    }, {
        name: 'sounLoop',
        type: 'int'
    }, {
        name: 'password',
        type: 'string'
    }, {
        name: 'hits',
        type: 'int'
    }, {
        name: 'comments',
        type: 'int'
    }, {
        name: 'isAuditing',
        type: 'int'
    }, {
        name: 'createDate',
        type: 'date'
    }, {
        name: 'modDate',
        type: 'date'
    }, {
        name: 'endPath',
        type: 'string'
    }, {
        name: 'background',
        type: 'string'
    }, {
        name: 'adWidth',
        type: 'int'
    }, {
        name: 'adHeight',
        type: 'int'
    }, {
        name: 'pubmemSize',
        type: 'int'
    }, {
        name: 'isValid',
        type: 'int'
    }, {
        name: 'isCommend',
        type: 'int'
    }, {
        name: 'pubPath',
        type: 'string'
    }, {
        name: 'pubType',
        type: 'int'
    }, {
        name: 'pubMagazineCount',
        type: 'int'
    }, {
        name: 'pubZoom',
        type: 'int'
    }, {
        name: 'pubBackgroudSize',
        type: 'int'
    }, {
        name: 'pubadSize',
        type: 'int'
    }, {
        name: 'pwdLapseDate',
        type: 'date'
    }, {
        name: 'pwdStartPage',
        type: 'int'
    }, {
        name: 'isCommpany',
        type: 'int'
    }]);
    
    var reader = new Ext.data.JsonReader({
        root: 'rows', // rows : 为服务器端传送过来的记录数值对象
        totalProperty: 'total' // results 为服务器端json传送过来的记录的行数, count(*)运算
    }, ['id', 'title', 'typeId', 'corpId', 'thumbPath', 'width', 'height', 'backgroundSound', 'backgroundRepeat', 'backgroundAlpha', 'sounLoop', 'password', 'hits', 'comments', 'isAuditing', 'createDate', 'modDate', 'endPath', 'background', 'adWidth', 'adHeight', 'pagesCount', 'pubmemSize', 'isValid', 'isCommend', 'pubPath', 'pubType', 'pubMagazineCount', 'pubZoom', 'pubBackgroudSize', 'pubadSize', 'pwdLapseDate', 'pwdStartPage', 'isCommpany']);
    
    function addHandler(){
        var newId;
        var conn = new Ext.data.Connection();
        conn.request({
            url: getNewIdUrl,
            params: {
                seqName: seqValue
            },
            success: function(resp, opt){
                newId = Ext.util.JSON.decode(resp.responseText).newId;
                openCreateWin(newId);
            },
            failure: function(resp, opt){
                Ext.Msg.alert('Error', '获取新id失败');
            }
        })
    }
    
    // add handle to deal with add action
    var openCreateWin = function(newId){
        var win;
        var f = new Ext.form.FormPanel({
            title: "添加电子书信息",
            height: 460,
            autoWidth: true,
            labelWidth: 75,
            labelAlign: "center",
            frame: true,
            defaultType: 'textfield',
            defaults: {
                width: 300
            },
            buttonAlign: 'center',
            items: [{
                xtype: 'tabpanel',
                plain: true,
                activeTab: 0,
                height: 450,
                autoWidth: true,
                deferredRender: false,
                defaults: {
                    bodyStyle: 'padding:10px'
                },
                items: [{
                    title: '基本信息',
                    layout: 'form',
                    defaults: {
                        width: 400
                    },
                    defaultType: 'textfield',
                    items: [{
                        fieldLabel: '编号',
                        name: 'corpPublication.id',
                        value: newId,
                        readOnly: true
                    }, {
                        fieldLabel: '书名称',
                        name: 'corpPublication.title'
                    }, {
                        fieldLabel: '电子书类型',
                        name: 'corpPublication.typeId'
                    }, {
                        fieldLabel: '企业ID',
                        name: 'corpPublication.corpId'
                    }, {
                        fieldLabel: '书路径',
                        name: 'corpPublication.thumbPath'
                    }, {
                        fieldLabel: '宽度',
                        name: 'corpPublication.width'
                    }, {
                        fieldLabel: '高度',
                        name: 'corpPublication.height'
                    }, {
                        fieldLabel: '背景音乐',
                        name: 'corpPublication.backgroundSound'
                    }, {
                        fieldLabel: '背景重复',
                        name: 'corpPublication.backgroundRepeat'
                    }, {
                        fieldLabel: '透明度',
                        name: 'corpPublication.backgroundAlpha'
                    }, {
                        fieldLabel: '背景乐音重复',
                        name: 'corpPublication.sounLoop'
                    }, {
                        fieldLabel: '密码',
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '点击率',
                        name: 'corpPublication.hits'
                    }, {
                        fieldLabel: '评论数',
                        name: 'corpPublication.comments'
                    }, {
                        fieldLabel: '透明度',
                        name: 'corpPublication.backgroundAlpha'
                    }, {
                        fieldLabel: '网站网址',
                        name: 'corpPublication.sounLoop'
                    }, new Ext.form.NumberField({
                        fieldLabel: '排序',
                        name: 'corpPublication.password'
                    }), new Ext.form.NumberField({
                        fieldLabel: '是否有效',
                        name: 'corpPublication.isValid'
                    })]
                }, {
                    title: '其他信息',
                    layout: 'form',
                    defaults: {
                        width: 400
                    },
                    defaultType: 'textfield',
                    
                    items: [{
                        fieldLabel: '企业登录名',
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '密码',
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '联系人',
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '网站域名',
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '行业分类',
                        name: 'corpPublication.password'
                    }, new Ext.form.NumberField({
                        fieldLabel: '是否推荐',
                        name: 'corpPublication.password'
                    }), {
                        fieldLabel: '视频',
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: 'ICP号',
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '公司LOGO',
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '模板ID',
                        name: 'corpPublication.password'
                    }, new Ext.form.NumberField({
                        fieldLabel: '企业排序',
                        name: 'corpPublication.password'
                    })]
                }, {
                    cls: 'x-plain',
                    title: '公司简介',
                    layout: 'fit',
                    items: {
                        xtype: 'htmleditor',
                        id: 'corpPublication.password',
                        fieldLabel: '公司简介'
                    }
                }, {
                    cls: 'x-plain',
                    title: '主营产品',
                    layout: 'fit',
                    items: {
                        xtype: 'htmleditor',
                        id: 'corpPublication.password',
                        fieldLabel: '主营产品'
                    }
                }]
            }],
            
            buttons: [{
                text: "提交",
                handler: s
            }, {
                text: "重置",
                handler: function(){
                    updateForm.form.reset();
                }
            }]
        
        });
        
        function s(){
            f.form.submit({
                waitTitle: "请稍候",
                waitMsg: "正在提交表单数据，请稍候.......",
                url: addMsg,
                success: function(action, form){
                    Ext.MessageBox.alert('提示信息', '数据已经成功提交！', function close(){
                        win.close();
                        location.replace(location.href);
                    });
                },
                failure: function(f, a){
                    Ext.MessageBox.alert('提示信息', "数据添加失败！");
                }
            });
            
        }
        
        if (!win) {
            win = new Ext.Window({
                title: '信息录入',
                width: 700,
                height: 500,
                x: 100,
                y: 50,
                closeAction: 'close',
                plain: true,
                modal: true,
                items: f
            });
        }
        win.show();
        
    };
    
    // 更新表单
    var updateFormHandler = function(){
        editor.stopEditing();
        var s = grid.getSelectionModel().getSelected();
        if (!s) 
            return false;
        var win;
        var updateForm = new Ext.form.FormPanel({
            title: "添加信息",
            height: 460,
            autoWidth: true,
            labelWidth: 75,
            labelAlign: "center",
            frame: true,
            defaultType: 'textfield',
            defaults: {
                width: 300
            },
            buttonAlign: 'center',
            items: [{
                xtype: 'tabpanel',
                plain: true,
                activeTab: 0,
                height: 450,
                autoWidth: true,
                deferredRender: false,
                defaults: {
                    bodyStyle: 'padding:10px'
                },
                items: [{
                    title: '基本信息',
                    layout: 'form',
                    defaults: {
                        width: 400
                    },
                    defaultType: 'textfield',
                    items: [{
                        fieldLabel: '编号',
                        name: 'corpPublication.id',
                        value: s.get("id"),
                        readOnly: true
                    }, {
                        fieldLabel: '公司名称',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '公司地址',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '邮编号',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '联系电话',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '传真',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '邮箱',
                        value: s.get("password"),
                        name: 'corpPublication.password',
                        vtype: 'email'
                    }, {
                        fieldLabel: '联系QQ',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '联系手机',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '网站名称',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '网站网址',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, new Ext.form.NumberField({
                        fieldLabel: '排序',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }), new Ext.form.NumberField({
                        fieldLabel: '是否有效',
                        value: s.get("isValid"),
                        name: 'corpPublication.isValid'
                    })]
                }, {
                    title: '其他信息',
                    layout: 'form',
                    defaults: {
                        width: 400
                    },
                    defaultType: 'textfield',
                    
                    items: [{
                        fieldLabel: '企业登录名',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '密码',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '联系人',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '网站域名',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '行业分类',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, new Ext.form.NumberField({
                        fieldLabel: '是否推荐',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }), {
                        fieldLabel: '视频',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: 'ICP号',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '公司LOGO',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, {
                        fieldLabel: '模板ID',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    }, new Ext.form.NumberField({
                        fieldLabel: '企业排序',
                        value: s.get("password"),
                        name: 'corpPublication.password'
                    })]
                }, {
                    cls: 'x-plain',
                    title: '公司简介',
                    layout: 'fit',
                    items: {
                        xtype: 'htmleditor',
                        id: 'corpPublication.password',
                        fieldLabel: '公司简介',
                        value: s.get("password")
                    }
                }, {
                    cls: 'x-plain',
                    title: '主营产品',
                    layout: 'fit',
                    items: {
                        xtype: 'htmleditor',
                        id: 'corpPublication.password',
                        fieldLabel: '主营产品',
                        value: s.get("password")
                    }
                }]
            }],
            
            buttons: [{
                text: "提交",
                handler: updateSubmit
            }, {
                text: "重置",
                handler: function(){
                    updateForm.form.reset();
                }
            }]
        });
        
        function updateSubmit(){
            updateForm.form.submit({
                waitTitle: "请稍候",
                waitMsg: "正在提交表单数据，请稍候.......",
                url: modifyMsg,
                success: function(action, form){
                    Ext.MessageBox.alert('提示信息', '数据已经成功提交！', function close(){
                        win.close();
                        location.replace(location.href); // 重新载入数据
                    });
                },
                failure: function(f, a){
                    Ext.MessageBox.alert('提示信息', "数据添加失败！");
                }
            });
            
        }
        
        if (!win) {
            win = new Ext.Window({
                title: '信息更改',
                width: 700,
                height: 500,
                x: 100,
                y: 50,
                closeAction: 'close',
                plain: true,
                modal: true,
                items: updateForm
            });
        }
        win.show();
    }
    
    // -------------------------下面无需改动-----------------------------------------------------------
    
    var store = new Ext.data.Store({
        url: findall,
        reader: reader
    });
    
    var editor = new Ext.ux.grid.RowEditor({
        saveText: '保存',
        cancelText: '取消',
        listeners: {
            afteredit: function(e){
                saveHandler(e);
            }
        }
    });
    
    var grid = new Ext.grid.GridPanel({
        renderTo: renderTo,
        frame: true,
        title: gridTitle,
        autoScroll: true,
        width: "auto",
        height: gridHeight,
        store: store,
        // plugins : [ editor ],
        sm: sm,
        cm: cm,
        tbar: [{
            text: ' 增加 ',
            handler: addHandler
        }, {
            ref: '../removeBtn',
            text: ' 删除 ',
            disabled: true,
            handler: function(){
                delHandler();
            }
        }, {
            ref: '../updateBtn',
            text: ' 修改 ',
            disabled: true,
            handler: function(){
                updateFormHandler();
            }
        }],
        viewConfig: {
            columnsText: "显示/隐藏列",
            sortAscText: "正序排列",
            sortDescText: "倒序排列"
        },
        bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: store,
            displayInfo: true,
            afterPageText: ' 共 {0} 页',
            beforePageText: '页 ',
            firstText: '第一页 ',
            prevText: '前一页',
            nextText: '下一页',
            lastText: '最后一页',
            refreshText: '刷新',
            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
            emptyMsg: "没有记录"
        })
    
    });
    store.load({
        params: {
            start: 0,
            limit: pageSize
        }
    });
    
    grid.getSelectionModel().on('selectionchange', function(sm){
        grid.removeBtn.setDisabled(sm.getCount() < 1);
        grid.updateBtn.setDisabled(sm.getCount() != 1);
    });
    
    // 删除的处理函数
    var delHandler = function(){
        editor.stopEditing();
        var s = grid.getSelectionModel().getSelections();
        if (!s) 
            return false;
        Ext.Msg.show({
            title: '删除',
            buttons: Ext.MessageBox.YESNOCANCEL,
            msg: '确认删除？',
            fn: function(btn){
				if(btn == "no" || btn == "cancel") return;
                var ids = "";
                for (var i = 0; i < s.length; i++) { // 要删除的记录ID拼串
                    ids = ids + s[i].get(identityName) + ",";
                }
                var conn = new Ext.data.Connection();
                conn.request({
                    url: deleteMsg,
                    params: {
                        delIds: ids
                    },
                    success: function(resp, opt){
                        for (var i = 0; i < s.length; i++) { // 把store中相应的记录删掉
                            grid.getStore().remove(s[i]);
                        }
                        store.reload();
                    },
                    failure: function(resp, opt){
                        Ext.Msg.alert('错误', '删除记录失败！');
                    }
                });
            }
        });
    }
});
